package org.pentaho.di.core.database.util;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettlePluginException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.xml.XMLHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tanp@yzbdl.ac.cn
 */
public class DriverDefPathUtil {

    public static DriverDefPathUtil dabaseConfigUtil = new DriverDefPathUtil();

    public static DriverDefPathUtil getInstance() {
        return DriverDefPathUtil.dabaseConfigUtil;
    }

    public List<DriverDefPathDict> dabaseConfigs;

    public DriverDefPathUtil() {
        List<DriverDefPathDict> dabaseConfigs = new ArrayList<>();

        // Scan the native database pathconfig...
        String xmlFile = Const.XML_FILE_KETTLE_DATABASE_DRIVER_PATH;

        // Load the path for this file...
        try {
            InputStream inputStream = getClass().getResourceAsStream(xmlFile);
            if (inputStream == null) {
                inputStream = getClass().getResourceAsStream("/" + xmlFile);
            }
            if (inputStream == null) {
                throw new KettlePluginException("Unable to find native kettle database types definition file: " + xmlFile);
            }
            Document document = XMLHandler.loadXMLFile(inputStream, null, true, false);

            Node repsNode = XMLHandler.getSubNode(document, "database-configs");
            List<Node> repsNodes = XMLHandler.getNodes(repsNode, "database-config");
            for (Node repNode : repsNodes) {
                DriverDefPathDict dict = new DriverDefPathDict();
                dict.setMetaId(XMLHandler.getTagValue(repNode, "meta-id"));
                dict.setName(XMLHandler.getTagValue(repNode, "name"));
                dict.setDefaultDriverPath(XMLHandler.getTagValue(repNode, "driver-def-path"));
                dict.setPluginFolder(XMLHandler.getTagValue(repNode, "plugin-folder"));
                dabaseConfigs.add(dict);
            }
        } catch (KettleXMLException | KettlePluginException e) {
            try {
                throw new KettlePluginException("Unable to read the kettle database types XML config file: " + xmlFile, e);
            } catch (KettlePluginException ex) {
                ex.printStackTrace();
            }
        }
        this.dabaseConfigs = dabaseConfigs;
    }


    public List<DriverDefPathDict> getDabaseConfigs() {
        return dabaseConfigs;
    }

    public void setDabaseConfigs(List<DriverDefPathDict> dabaseConfigs) {
        this.dabaseConfigs = dabaseConfigs;
    }

    /**
     * 根据metaId获取默认路径值
     *
     * @param metaId
     * @return
     */
    public String getPathBymetaId(String metaId) {
        for (DriverDefPathDict driverPathDict : dabaseConfigs) {
            if (driverPathDict.getMetaId().equals(metaId)) {
                return driverPathDict.getDefaultDriverPath();
            }
        }
        return null;
    }

    /**
     * 根据metaId获取插件所在目录
     *
     * @param metaId
     * @return
     */
    public String getPluginFolderBymetaId(String metaId) {
        for (DriverDefPathDict driverPathDict : dabaseConfigs) {
            if (driverPathDict.getMetaId().equals(metaId)) {
                return driverPathDict.getPluginFolder();
            }
        }
        return null;
    }


}